package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.pnikosis.materialishprogress.ProgressWheel;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.custom.polites.GestureImageView;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ImageSaveUtil;
import reduce.fz.com.reduce.util.Utils;
import reduce.fz.com.reduce.util.http.ImageLoader;

/**
 * 图片播放器
 *
 * @auth lipf on 2015/1/29.
 */
public class ImageDisplayPagerAdapter extends PagerAdapter implements View.OnClickListener {

    private List<String> mDisplayImage;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private OnCloseImageDisplayListener mOnCloseImageDisplayListener;
    private ImageLoader mImageLoader = HttpUtils.getImageLoader();
    private String mUserName;

    public ImageDisplayPagerAdapter(Context context, String userName, OnCloseImageDisplayListener onCloseImageDisplayListener) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mDisplayImage = new ArrayList<>();
        mOnCloseImageDisplayListener = onCloseImageDisplayListener;
        mUserName = userName;
    }

    public void replaceVersionDisplay(List<String> resList) {
        mDisplayImage.clear();
        mDisplayImage.addAll(resList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return mDisplayImage.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final String imagePath = mDisplayImage.get(position);
        View view = mLayoutInflater.inflate(R.layout.view_image_display_item, container, false);
        view.setOnClickListener(this);
        final GestureImageView imageView = (GestureImageView) view.findViewById(R.id.version_display_image);
        final ProgressWheel mProgressWheel = (ProgressWheel) view.findViewById(R.id.version_display_image_progress_wheel);

        final TextView downpic = (TextView) view.findViewById(R.id.tv_download_pic);
        downpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.showToast(R.string.pic_down_success);
                downpic.setText(R.string.pic_down_text_has);
                downpic.setClickable(false);

                new Thread() {
                    @Override
                    public void run() {
                       // ImageSaveUtil.downLoadPic(mContext, imageView, "ID:" + mUserName);
                    }
                }.start();

            }
        });
        downpic.setClickable(false);
        imageView.setOnClickListener(this);

        if (!TextUtils.isEmpty(imagePath)) {
            mProgressWheel.setVisibility(View.VISIBLE);
            imageView.setTag(imagePath);
            mImageLoader.get(imagePath, new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    try {
                        if (response == null || response.getBitmap() == null) {
                            return;
                        }
                        String url = response.getRequestUrl();
                        if (imagePath.equals(url)) {
                            mProgressWheel.setVisibility(View.GONE);
                            Bitmap bitmap = response.getBitmap();
                            imageView.setImageBitmap(bitmap);
                        }
                        downpic.setClickable(true);
                    } catch (Exception e) {
                        mProgressWheel.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    mProgressWheel.setVisibility(View.GONE);
                }
            });
        }
        container.addView(view);
        return view;
    }

    @Override
    public void onClick(View v) {
        mOnCloseImageDisplayListener.onCloseVersionDisplay();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == (View) object;
    }

    public interface OnCloseImageDisplayListener {
        public void onCloseVersionDisplay();
    }
}
